package org.jeecg.desen.run.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.desen.run.builder.DesBuilder;
import org.jeecg.modules.rule.entity.Autossc;
import org.jeecg.modules.rule.entity.OpersscFo;
import org.jeecg.modules.rule.service.IAutosscService;
import org.jeecg.modules.rule.service.IOpersscFoService;
import org.jeecg.modules.run.entity.Run;
import org.jeecg.modules.run.service.IRunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/des/run")
@Slf4j
public class DesRunController {

    @Autowired
    DesBuilder desBuilder;

    @Autowired
    IRunService iRunService;

    @Autowired
    IAutosscService iAutosscService;

    @Autowired
    IOpersscFoService iOpersscFoService;

    /**
     * 执行自动运行脱敏转换任务
     *
     * @return
     */
    @RequestMapping(value = "/auto")
    public Result<Object> autoDay(String pro_num, String runDate) {
        log.info("auto:自动脱敏,频次=" + pro_num + ",日期=" + runDate);
        if (StringUtils.isEmpty(pro_num) || StringUtils.isEmpty(runDate)) {
            log.error("auto-error:自动脱敏,频次=" + pro_num + ",日期=" + runDate);
            return Result.ok(false);
        }

        Date parseRunDate;
        try {
            parseRunDate = DateUtil.parseDate(runDate);
        } catch (Exception e) {
            String errorStr = "auto:自动脱敏,日期格式错误,日期=" + runDate;
            log.info(errorStr);
            return Result.error(errorStr);
        }

        return Result.ok(desBuilder.builderAuto(pro_num, parseRunDate).run());
    }


    /**
     * 执行手动运行脱敏转换任务
     *
     * @return
     */
    @GetMapping(value = "/manual")
    public Result<Object> manual(String sscid, String runDate, String endDate) {
        log.info("manual:手动脱敏,订阅id=" + sscid);
        if (StringUtils.isEmpty(sscid) || StringUtils.isEmpty(runDate)) {
            log.error("manual:手动脱敏,订阅id==null,日期=" + runDate);
            return Result.ok(false);
        }

        Date paseRunDate;
        Date paseEndDate = null;
        try {
            paseRunDate = DateUtil.parseDate(runDate);
            if (StringUtils.isNotEmpty(endDate) && !"null".equals(endDate)) {
                paseEndDate = DateUtil.parseDate(endDate);
            }
        } catch (Exception e) {
            String errorStr = "manual:手工脱敏,日期格式错误,日期=" + runDate;
            log.info(errorStr);
            return Result.error(errorStr);
        }

        //如果手工脱敏是日期周期
        if (StringUtils.isNotEmpty(endDate)) {
            return Result.ok(desBuilder.builderManual(sscid, paseRunDate, paseEndDate).run());
        } else {
            return Result.ok(desBuilder.builderManual(sscid, paseRunDate).run());
        }
    }

    /**
     * 执行手动运行脱敏转换任务
     *
     * @return
     */
    @GetMapping(value = "/reRun")
    public Result<Object> reRun(String reRunId) {
        log.info("reRun:重跑=" + reRunId);
        String error = "";
        if (StringUtils.isEmpty(reRunId)) {
            log.error("reRun:重跑,运行id==null");
            return Result.ok(false);
        }

        //获取运行监控信息
        Run run = iRunService.getById(reRunId);
        if(null == run){
            log.error("reRun:没有找到运行数据,运行id=" + reRunId);
            return Result.ok(false);
        }

        if(1 == run.getFdType()){//自动的运行重跑
            //获取订阅信息 获取频次
            Autossc autossc = getAutosscList(run.getFdSscid());
            if(null == autossc){
                error = "reRun:没有找到自动脱敏订阅信息,运行id=" + reRunId;
                log.error(error);
                return Result.ok(error);
            }
            desBuilder.builderAuto(autossc.getFdProNum(), run.getFdBuStartDate()).reRun(reRunId);
        } else if(2 == run.getFdType()){//自动的运行重跑
            //获取批次信息
            OpersscFo opersscFo = getManualSscList(run.getFdSscid());
            if(null == opersscFo){
                error = "reRun:没有找到手工脱敏订阅信息,运行id=" + reRunId;
                log.error(error);
                return Result.ok(error);
            }
            //如果手工脱敏是日期周期
            if (null != run.getFdBuEndDate()) {
                desBuilder.builderManual(opersscFo.getId(), run.getFdBuStartDate(), run.getFdBuEndDate()).reRun(reRunId);
            } else {
                desBuilder.builderManual(opersscFo.getId(), run.getFdBuStartDate()).reRun(reRunId);
            }
        }

        return Result.ok(true);
    }

    private Autossc getAutosscList(String fd_sscid) {
        //获取频次对应的订阅信息 ，当个频次可以对应
        return iAutosscService.getById(fd_sscid);
    }

    private OpersscFo getManualSscList(String sscId) {
        //获取频次对应的订阅信息 ，当个频次可以对应
        QueryWrapper<OpersscFo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", sscId);
        queryWrapper.eq("fd_status", 1);
        return iOpersscFoService.getOne(queryWrapper);
    }

}
